home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C11 / Autocc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.1 KB  |  52 lines

  1. //: C11:Autocc.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Automatic copy-constructor
  7. #include <iostream>
  8. #include <cstring>
  9. using namespace std;
  10.  
  11. class WithCC { // With copy-constructor
  12. public:
  13.   // Explicit default constructor required:
  14.   WithCC() {}
  15.   WithCC(const WithCC&) {
  16.     cout << "WithCC(WithCC&)" << endl;
  17.   }
  18. };
  19.  
  20. class WoCC { // Without copy-constructor
  21.   static const int bsz = 30;
  22.   char buf[bsz];
  23. public:
  24.   WoCC(const char* msg = 0) {
  25.     memset(buf, 0, bsz);
  26.     if(msg) strncpy(buf, msg, bsz);
  27.   }
  28.   void print(const char* msg = 0) const {
  29.     if(msg) cout << msg << ": ";
  30.     cout << buf << endl;
  31.   }
  32. };
  33.  
  34. class Composite {
  35.   WithCC withcc; // Embedded objects
  36.   WoCC wocc;
  37. public:
  38.   Composite() : wocc("Composite()") {}
  39.   void print(const char* msg = 0) {
  40.     wocc.print(msg);
  41.   }
  42. };
  43.  
  44. int main() {
  45.   Composite c;
  46.   c.print("contents of c");
  47.   cout << "calling Composite copy-constructor"
  48.        << endl;
  49.   Composite c2 = c;  // Calls copy-constructor
  50.   c2.print("contents of c2");
  51. } ///:~
  52.